package LeetCode.month01.day26;

import java.util.Arrays;

public class No1652 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(decrypt(new int[]{5,7,1,4},3)));
    }
    public static int[] decrypt(int[] code, int k) {
        int len = code.length;
        int[] arr = new int[len];
        if(k==0){
            Arrays.fill(arr,0);
        }else if(k>0){
            for (int i = 0; i < k; i++) {
                arr[0]+=code[i+1];
            }
            int index = 1;
            for (int i = k+1; index<len; i++,index++) {
                arr[index] = arr[index-1]+code[i%len]-code[(i-k)%len];
            }
        }else {
            k=-k;
            for (int i = 0; i < k; i++) {
                arr[0]+=code[len-k+i];
            }
            int index = 1;
            for (int i = len; index<len; i++,index++) {
                arr[index] = arr[index-1]+code[i%len]-code[(i-k)%len];
            }
        }
        return arr;
    }
}
